using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;

namespace Chess
{
    public class AL_ArrImage : ArrayList
    {
        public Image[] this[int index]
        {
            get
            {
                return (Image[])base[index];
            }
            set
            {
                base[index] = value;
            }
        }
    }

    public class Util
    {
        public static Image ThucHienDiChuyen(Graphics g, Color mauXoa, Point vtBanCo, Point src, Point des, Image bkDau, Image bkCuoi, Bitmap th, Bitmap thSau, int kcStep, int thoigian)
        {
            int doPhong = 6;
            Bitmap tempDau = new Bitmap(bkDau);
            Graphics gtemp = Graphics.FromImage(tempDau);
            ImageAttributes att = new ImageAttributes();
            att.SetColorKey(mauXoa, mauXoa);
            while (src != des)
            {
                Thread.Sleep(thoigian);
                if (src.X != des.X)
                {
                    src.X = (src.X > des.X) ? (src.X - kcStep) : (src.X + kcStep);
                }
                if (src.Y != des.Y)
                {
                    src.Y = (src.Y > des.Y) ? (src.Y - kcStep) : (src.Y + kcStep);
                }

                gtemp.DrawImage(bkDau, 0, 0);
                gtemp.DrawImage(th, new Rectangle(src.X - (doPhong/2), src.Y - (doPhong/2), th.Width + doPhong, th.Height + doPhong), 0, 0, th.Width, th.Height, GraphicsUnit.Pixel, att);
                g.DrawImage(tempDau, vtBanCo.X, vtBanCo.Y);
            }
            gtemp.Dispose();
            tempDau.Dispose();

            Graphics gtempCuoi = Graphics.FromImage(bkCuoi);
            gtempCuoi.DrawImage(thSau, new Rectangle(src.X, src.Y, thSau.Width, thSau.Height), 0, 0, thSau.Width, thSau.Height, GraphicsUnit.Pixel, att);
            g.DrawImage(bkCuoi, vtBanCo.X, vtBanCo.Y);
            gtempCuoi.Dispose();
            Bitmap kq = new Bitmap(bkCuoi);
            return kq;
        }

        public static Image TaoBanCo(Hashtable dsQC, Image[] listImg, int dai, int cao, int ktOCo, int soOCo, Color mauXoa)
        {
            Image kq = new Bitmap(dai, cao);
            Graphics g = Graphics.FromImage(kq);
            g.DrawImage(listImg[0], new Rectangle(0, 0, dai, cao), new Rectangle(0, 0, dai, cao), GraphicsUnit.Pixel);
            ImageAttributes att = new ImageAttributes();
            att.SetColorKey(mauXoa, mauXoa);
            foreach (int each in dsQC.Keys)
            {
                int indexHinh = (int)dsQC[each];
                Point vtve = ConvertToaDo2ViTri(ktOCo, each, soOCo);
                g.DrawImage(listImg[indexHinh], new Rectangle(vtve.X, vtve.Y, ktOCo, ktOCo), 0, 0, listImg[indexHinh].Width, listImg[indexHinh].Height, GraphicsUnit.Pixel, att);
            }
            g.Dispose();
            return kq;
        }

        public static Image TaoNuocDiCoThe(Hashtable[] dsQC, Image[] dsImg, int csHinhNext, bool thTren, int dai, int cao, int ktOCo, int soOCo, Color mauXoa)
        {
            Image kq = TaoBanCo(dsQC[0], dsImg, dai, cao, ktOCo, soOCo, mauXoa);
            Graphics g = Graphics.FromImage(kq);
            ImageAttributes att = new ImageAttributes();
            att.SetColorKey(mauXoa, mauXoa);
            Point vtve = new Point();
            foreach (int each in dsQC[2].Keys)
            {
                int qc = (int)dsQC[2][each];
                vtve = ConvertToaDo2ViTri(ktOCo, each, soOCo);
                g.DrawImage(dsImg[qc], new Rectangle(vtve.X - 2, vtve.Y - 2, ktOCo + 4, ktOCo + 4), 0, 0, ktOCo, ktOCo, GraphicsUnit.Pixel, att);
            }

            AL_Int dsND = (AL_Int)dsQC[1]["dsNC"];
            if (thTren)
            {
                foreach (object each in dsQC[1].Keys)
                {
                    try
                    {
                        int vt = (int)each;
                        int qc = (int)dsQC[1][vt];
                        vtve = ConvertToaDo2ViTri(ktOCo, vt, soOCo);
                        g.DrawImage(dsImg[qc], new Rectangle(vtve.X, vtve.Y, ktOCo, ktOCo), 0, 0, ktOCo, ktOCo, GraphicsUnit.Pixel, att);
                    }
                    catch (System.Exception)
                    {
                    }
                }
                foreach (int each in dsND)
                {
                    vtve = ConvertToaDo2ViTri(ktOCo, each, soOCo);
                    g.DrawImage(dsImg[csHinhNext], new Rectangle(vtve.X, vtve.Y, ktOCo, ktOCo), 0, 0, ktOCo, ktOCo, GraphicsUnit.Pixel, att);
                }
            }
            else
            {
                foreach (int each in dsND)
                {
                    vtve = ConvertToaDo2ViTri(ktOCo, each, soOCo);
                    g.DrawImage(dsImg[csHinhNext], new Rectangle(vtve.X, vtve.Y, ktOCo, ktOCo), 0, 0, ktOCo, ktOCo, GraphicsUnit.Pixel, att);
                }

                foreach (object each in dsQC[1].Keys)
                {
                    try
                    {
                        int vt = (int)each;
                        int qc = (int)dsQC[1][vt];
                        vtve = ConvertToaDo2ViTri(ktOCo, vt, soOCo);
                        g.DrawImage(dsImg[qc], new Rectangle(vtve.X, vtve.Y, ktOCo, ktOCo), 0, 0, ktOCo, ktOCo, GraphicsUnit.Pixel, att);
                    }
                    catch (System.Exception)
                    {
                    }
                }
            }


            g.Dispose();
            return kq;
        }

        public static Point ConvertToaDo2ViTri(int ktQC, int vt, int soOCo)
        {
            Point kq = new Point();
            int y = vt / soOCo;
            int x = vt % soOCo;
            kq.X = x * ktQC;
            kq.Y = y * ktQC;
            return kq;
        }

        public static Point LayViTriTrenBanCo(int x, int y, int stX, int stY, int w, int h, int sW, int sH)
        {
            Point kq = new Point();
            x -= stX;
            y -= stY;
            if (x < 0 || x > sW || y < 0 || y > sH)
            {
                kq.X = -1;
                return kq;
            }

            kq.X = y / h;
            kq.Y = x / w;

            return kq;
        }
    }
}
